﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace WebUI.Lang
{
    public class AdlerBasePage:System.Web.UI.Page
    {
        private static MyResource resource;
        protected override void InitializeCulture()
        {
            string langId = string.Empty;
            var cookie = Request.Cookies["langId"];
            if (cookie != null && cookie.Value != null)
            {
                langId = cookie.Value;
            }
            else
            {
                langId = "0";
                cookie = new HttpCookie("langId");
                cookie.Value = "0";
                Request.Cookies.Add(cookie);
            }
            LoadResource(langId);
            base.InitializeCulture();
        }

        private void LoadResource(string langId)
        {
            string lang = "";
            if(langId=="1")
            {
                lang = "vi";
            }
            else
            {
                lang = "en";
            }
            resource = new MyResource();
            resource.xmldoc = new XmlDocument();
            string physicalPath = HttpContext.Current.Request.PhysicalApplicationPath + "lang\\" + lang + ".xml";

            resource.xmldoc.Load(physicalPath);
            resource.nodes = resource.xmldoc.GetElementsByTagName("obj");


        }
        /// <summary>
        /// Cái này lấy text từ xml ra, mà tao thấy nó chưa tối ưu, cần phải tối ưu nó
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetText(string obj)
        {
            if (resource != null)
            {
                string result = "";
                foreach (XmlNode node in resource.nodes)
                {
                    if (node.Attributes.Item(0).InnerText == obj)
                    {
                        result = node.ChildNodes[0].InnerText;
                        break;
                    }
                }
                return result;
            }
            return null;

        }
    }
}